package count.negative.quickstarter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class IntentAdapter extends ArrayAdapter<ResolveInfo> {

	PackageManager pm = getContext().getPackageManager();

	Map<String, Intent> map = new HashMap<String, Intent>();

	public IntentAdapter(Context context, int textViewResourceId, List<ResolveInfo> list) {
		super(context, textViewResourceId, list);

		for (int i = 0; i < list.size(); i++) {
			ActivityInfo applicationInfo = list.get(i).activityInfo;
			map.put(applicationInfo.packageName, pm.getLaunchIntentForPackage(applicationInfo.packageName));
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View tablerow = inflater.inflate(R.layout.tablerow, parent, false);
		final CheckBox checkBox = (CheckBox) tablerow.findViewById(R.id.checkBox1);
		final ImageView imageView = (ImageView) tablerow.findViewById(R.id.imageView1);
		final TextView textView = (TextView) tablerow.findViewById(R.id.textView1);

		ActivityInfo applicationInfo = MainActivity.resolveInfos.get(position).activityInfo;
		if (applicationInfo.packageName.equals("android") || "count.negative.quickstarter".equals(applicationInfo.packageName)) {
			checkBox.setClickable(false);
			textView.setTextColor(Color.GRAY);
		}
		checkBox.setTag(applicationInfo);
		checkBox.setChecked(isContained(MainActivity.selectedPackageNames, applicationInfo));
		checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				ActivityInfo tag = (ActivityInfo) checkBox.getTag();
				if (isChecked) {
					MainActivity.selectedPackageNames.add(tag.packageName);
				} else {
					MainActivity.selectedPackageNames.remove(tag.packageName);
				}

			}
		});
		textView.setText(getItem(position).loadLabel(pm));
		textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				checkBox.setChecked(!checkBox.isChecked());
			}
		});

		imageView.setImageDrawable(applicationInfo.loadIcon(pm));
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				checkBox.setChecked(!checkBox.isChecked());
			}
		});

		return tablerow;
	}

	private boolean isContained(List<String> selectedPackages, ActivityInfo applicationInfo) {
		for (String selectedPackage : selectedPackages) {
			if (selectedPackage.equals(applicationInfo.packageName)) {
				return true;
			}
		}
		return false;
	}

}
